package com.br.view;

import java.util.ArrayList;
import java.util.List;

import com.br.dao.MenuDao;
import com.br.dao.EmpresaDao;
import com.br.dao.UsuarioDao;
import com.br.model.Menu;
import com.br.model.Empresa;
import com.br.model.Usuario;

public class MenuView {
	
	private String geraAlteracao(String pCodMenu, String pCodEMpresa){
		
		//variaveis
		String vCodeLine                = null;
		List<Empresa>      listaEmpresa = new ArrayList<Empresa>();
		List<Menu>         listaMenu    = new ArrayList<Menu>();
		List<String> vColunas           = new ArrayList<String>();
		List<String> vValores           = new ArrayList<String>();
		
		//Objetos
		Empresa         empresa    = new Empresa();
		EmpresaDao      empresaDao = new EmpresaDao();
		Menu            menu       = new Menu();
		MenuDao	    	menuDao    = new MenuDao();
		
		//Carregando Listas
		vColunas.add("COD_MENU");
		vColunas.add("COD_EMPRESA");
		vValores.add(pCodMenu);
		vValores.add(pCodEMpresa);
		
		listaMenu = menuDao.consulta(vColunas, vValores);
		
		menu = listaMenu.get(0);
		
		//Monta Codigo
		vCodeLine  = "<div id=\"inclusaoMeioRodape\">";
		vCodeLine += "</br>";
		vCodeLine += "</br>";
		vCodeLine += " <form action=\"mnm.jsp\" method=\"post\">";
		vCodeLine += "  <p align=\"center\"><strong>Cadastro de Menu</strong></p>";
		vCodeLine += "  </br>";
		vCodeLine += "  <p><strong>&nbsp;&nbsp; Menu &nbsp;&nbsp;" +
					 "             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
					 "             <input name=\"txtMenu\" type=\"text\" size=\"75\" value=\""+menu.getNOM_MENU()+"\"></strong></p>";
		vCodeLine += " <p><strong>&nbsp;&nbsp;  Empresa  " +
        			 "             &nbsp;&nbsp;&nbsp;" +
        			 "			   <select style=width:306px name=\"txtEmpresa\">";
		vCodeLine += " <option value=\"null\" size=\"45\"></option>";

		listaEmpresa = empresaDao.consulta(null, null);
		
		for (int i = 0; i < listaEmpresa.size(); i++){
			empresa = listaEmpresa.get(i);
			
			if (menu.getCOD_EMPRESA() == empresa.getCOD_EMPRESA()){
				vCodeLine += " <option value=\""+empresa.getCOD_EMPRESA()+"\" size=\"45\" selected>"+empresa.getNOM_EMPRESA()+"</option>";
			}else{
				vCodeLine += " <option value=\""+empresa.getCOD_EMPRESA()+"\" size=\"45\">"+empresa.getNOM_EMPRESA()+"</option>";
			}	
		}
		
		vCodeLine += "  </select></strong></p>";
		
		vCodeLine += "  <p align=\"center\"><input type=\"submit\" value=\"    Salvar    \"></p>";   
		vCodeLine += " </form>";
		vCodeLine += "</br>";
		vCodeLine += "</div>";
		
		return vCodeLine; 
	}	
	
	
	private String geraCadastro(){
		
		//variaveis
		String vCodeLine                = null;
		List<Empresa>      listaEmpresa = new ArrayList<Empresa>();
		//List<Departamento> listaDepto   = new ArrayList<Departamento>();
		
		//Objetos
		Empresa         empresa    = new Empresa();
		EmpresaDao      empresaDao = new EmpresaDao();
		//Departamento    depto      = new Departamento();
		//DepartamentoDao deptoDao   = new DepartamentoDao();
		//Carregando Listas
				
		//Monta Codigo
		vCodeLine  = "<div id=\"inclusaoMeioRodape\">";
		vCodeLine += "</br>";
		vCodeLine += "</br>";
		vCodeLine += " <form action=\"mnm.jsp\" method=\"post\">";
		vCodeLine += "  <p align=\"center\"><strong>Cadastro de Menu</strong></p>";
		vCodeLine += "  </br>";
		vCodeLine += "  <p><strong>&nbsp;&nbsp; Menu &nbsp;&nbsp;" +
					 "             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
					 "             <input name=\"txtMenu\" type=\"text\" size=\"75\"></strong></p>";
		vCodeLine += " <p><strong>&nbsp;&nbsp;  Empresa  " +
		             "             &nbsp;&nbsp;&nbsp;" +
				     "			   <select style=width:306px name=\"txtEmpresa\">";
		vCodeLine += " <option value=\"null\" size=\"45\"></option>";
		
		listaEmpresa = empresaDao.consulta(null, null);
		
		for (int i = 0; i < listaEmpresa.size(); i++){
			empresa = listaEmpresa.get(i);
			vCodeLine += " <option value=\""+empresa.getCOD_EMPRESA()+"\" size=\"45\">"+empresa.getNOM_EMPRESA()+"</option>";
		}
		
		vCodeLine += "  </select></strong></p>";
		
		vCodeLine += "  <p align=\"center\"><input type=\"submit\" value=\"    Salvar    \"></p>";   
		vCodeLine += " </form>";
		vCodeLine += "</br>";
		vCodeLine += "</div>";
		
		return vCodeLine; 
	}	
	
	private String geraGrid(){
		
		//variaveis
		String vCodeLine           = null;
		List<Menu> listaMenu       = null;
		List<Empresa> listaEmpresa = new ArrayList<Empresa>();
		List<String> vColunas      = new ArrayList<String>();
		List<String> vValores      = new ArrayList<String>();
				
		//Instanciando objetos
		MenuDao    MenuDao = new MenuDao();
		EmpresaDao empDao  = new EmpresaDao();
		Empresa    emp	   = new Empresa();
		
		//Add valores ao arryList
		listaMenu = MenuDao.consulta(null, null);

		//Montando Codigo
		vCodeLine  = "<ul id=\"iconbar\">";
		vCodeLine += " <li><a href=\"mn02.jsp\">";
		vCodeLine += " <img src=\"sliding/sel.gif\" alt=\"\">";
		vCodeLine += " <span>Cadastrar Menu</span></a>";
		vCodeLine += " </li>";
		vCodeLine += "</ul>";		
		vCodeLine += "<br><br>";
		vCodeLine += "<h2><font color=\"#F3F3F3\"><strong>Cadastro de Menu</strong></font></h2>"; 
		vCodeLine += "<table width=\"75%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" bordercolor=\"#CCCCCC\" bgcolor=\"#CCCCCC\">";
		vCodeLine += "<tr bgcolor=\"#708090\" align=\"center\">";
		vCodeLine += "<td height=\"50\" nowrap><h3><strong>&nbsp;&nbsp;Codigo&nbsp;</strong></h3></td>";
		vCodeLine += "<td height=\"50\" nowrap><h3><strong>&nbsp;&nbsp;Menu&nbsp;</strong></h3></td>";
		vCodeLine += "<td height=\"50\" nowrap><h3><strong>&nbsp;&nbsp;Empresa&nbsp;</strong></h3></td>";
		vCodeLine += "<td height=\"30\" nowrap><h3><strong>&nbsp;&nbsp;</strong></h3></td>";
		vCodeLine += "<td height=\"30\" nowrap><h3><strong>&nbsp;&nbsp;</strong></h3></td>";
		vCodeLine += "</tr>";
		
		for (int i=0; i < listaMenu.size(); i++){
			
			Menu menu = listaMenu.get(i);
		
			if( i % 2 == 0){
				vCodeLine += "<tr align=\"center\" bgcolor=\"#F3F3F3\" bordercolor=\"#CCCCCC\">";
			}else{
				vCodeLine += "<tr align=\"center\" bgcolor=\"#E1E1E1\" bordercolor=\"#CCCCCC\">";
			}
			vCodeLine += "<td height=\"30\" nowrap><font size =\"2\"><b>&nbsp;"+menu.getCOD_MENU()+"&nbsp;</b></font></td>";
			vCodeLine += "<td height=\"30\" nowrap><font size =\"2\"><b>&nbsp;"+menu.getNOM_MENU()+"&nbsp;</b></font></td>";
			
			vColunas.clear();
			vColunas.add("COD_EMPRESA");
			
			vValores.clear();
			vValores.add(Integer.toString(menu.getCOD_EMPRESA()));
			
			listaEmpresa = empDao.consulta(vColunas, vValores);
			
			emp = listaEmpresa.get(0);
			
			vCodeLine += "<td height=\"30\" nowrap><font size =\"2\"><b>&nbsp;"+emp.getNOM_EMPRESA()+"&nbsp;</b></font></td>";
			
			vCodeLine += "<td height=\"30\" nowrap><font size =\"2\"><b>&nbsp;&nbsp;<a href=\"mn03.jsp?id="+menu.getCOD_MENU()+"&id2="+menu.getCOD_EMPRESA()+"\"><img src=\"resources/btn_alterar.png\"></a>&nbsp;&nbsp;</b></font></td>";
			vCodeLine += "<td height=\"30\" nowrap><font size =\"2\"><b>&nbsp;&nbsp;<a href=\"mn04.jsp?id="+menu.getCOD_MENU()+"&id2="+menu.getCOD_EMPRESA()+"\"><img src=\"resources/excluir.png\" name = \"excluir\"></a>&nbsp;&nbsp;</b></font></td>";
			vCodeLine += "</tr>";
		}
		vCodeLine +="</table>";
		
		return vCodeLine;
	}
	
	public String getGrid(){
		return geraGrid();
	}
	
	public String getAdd(){
		return geraCadastro();
	}	
	
	public String getAlter(String pCodMenu,String pCodEmpresa){
		return geraAlteracao(pCodMenu, pCodEmpresa);
	}
	
}
